package com.hdy.orderforthree.pos;

import android.content.Context;

import com.hdy.common.ObjectSharedPreference;
import com.hdy.net.order.DGJOrder;
import com.hdy.net.order.NetCallback;
import com.hdy.net.order.OrderUrl;
import com.hdy.net.order.bean.BasicDataRequest;
import com.hdy.net.order.bean.ConfirmOrderRequest;
import com.hdy.net.order.bean.DishBean;
import com.hdy.net.order.bean.LoginBean;
import com.hdy.net.order.bean.LoginDataRequest;
import com.hdy.net.order.bean.OrderBean;

import java.util.ArrayList;

public class DGJPos implements IPos<DishBean> {

    private ConfirmOrderRequest mConfirmOrderRequest;
    private Context mContext;
    private LoginBean loginBean;
    private DGJOrder mDGJOrder;

    public DGJPos(Context context) {
        mContext = context;
        ObjectSharedPreference preference = new ObjectSharedPreference(mContext);
        loginBean = preference.get(LoginBean.class);
        if (loginBean != null) {
            OrderUrl.DGJ_DOMAIN = loginBean.getIp();
        }
        mDGJOrder = new DGJOrder();
    }

    @Override
    public void login(LoginBean loginBean, NetCallback callback) {
        LoginDataRequest request = new LoginDataRequest();
        request.setWaiter(loginBean);
        mDGJOrder.login(request, callback);
    }

    @Override
    public void order(Object orderRequestData, NetCallback callback) {

    }

    @Override
    public void getOrderInfo(String orderId, NetCallback callback) {

    }

    @Override
    public void getAllMenus(NetCallback callback) {
        BasicDataRequest request = new BasicDataRequest();
        mDGJOrder.getBasicData(request, callback);
    }

    @Override
    public synchronized void addCart(DishBean dishBean) {
        initCart();
        dishBean.setQty(dishBean.getQty() + 1);
        if (!mConfirmOrderRequest.getDishes().contains(dishBean)) {//购物车中不存在就加入
            mConfirmOrderRequest.getDishes().add(dishBean);
        }

        mConfirmOrderRequest.getOrder().setTotalQty(mConfirmOrderRequest.getOrder().getTotalQty() + 1);
    }

    @Override
    public synchronized void removeCart(DishBean dishBean) {
        initCart();
        if (dishBean.getQty() > 0) {
            dishBean.setQty(dishBean.getQty() - 1);
            mConfirmOrderRequest.getOrder().setTotalQty(mConfirmOrderRequest.getOrder().getTotalQty() - 1);
        }

        if (dishBean.getQty() == 0) {//数量为零，则从购物车中移除
            mConfirmOrderRequest.getDishes().remove(dishBean);
        }
    }

    private void initCart() {
        if (mConfirmOrderRequest == null) {
            mConfirmOrderRequest = new ConfirmOrderRequest();
            mConfirmOrderRequest.setOrder(new OrderBean());
            mConfirmOrderRequest.setDishes(new ArrayList<>());
        }
    }

    public ConfirmOrderRequest getConfirmOrderRequest() {
        return mConfirmOrderRequest;
    }

    @Override
    public synchronized void removeCartByDish(DishBean dishBean) {
        initCart();
        if (dishBean != null && !dishBean.isConfirmed()) {//移除菜品
            mConfirmOrderRequest.getDishes().remove(dishBean);
            mConfirmOrderRequest.getOrder().setTotalQty(mConfirmOrderRequest.getOrder().getTotalQty() - dishBean.getQty());
            dishBean.setQty(0.0f);
        }
    }

    @Override
    public LoginBean getLoginBean() {
        return loginBean;
    }

    @Override
    public void saveLoginBean(LoginBean bean) {
        loginBean = bean;
        ObjectSharedPreference preference = new ObjectSharedPreference(mContext);
        preference.save(bean);
    }
}
